<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/29
 * Time: 22:34
 */

namespace Organize\Policies;

use Platform\Models\Organize\Leader;
use Platform\Models\Organize\Organize;
use Platform\System\Tools\ResultTool;

class LeaderPolicy
{

    /**
     * 判断是否可以查看组织成员信息
     * @param Leader $leader
     * @param Leader $leader_target
     * @return bool
     */
    public function show(Leader $leader,Leader $leader_target)
    {
        if (optional($leader->organize)->id !== optional($leader_target->organize)->id) myResult()->stop(ResultTool::CODE_TARGET_WRONG,'该成员不存在');
        return true;
    }

    /**
     * 判断是否可以更新此组织成员信息
     * @param Leader $leader
     * @param Leader $leader_target
     * @return bool
     */
    public function update(Leader $leader,Leader $leader_target)
    {
        if (optional($leader->organize)->id !== optional($leader_target->organize)->id) myResult()->stop(ResultTool::CODE_NO_RIGHT);
        if ($leader->isOrganizeAdmin()) return true;
        if ($leader->id === $leader_target->id){
            return true;
        };
        myResult()->stop(ResultTool::CODE_NO_RIGHT);
    }

    /**
     * 判断是否可以删除此管理员
     * @param Leader $leader
     * @param Leader $leader_target
     * @return bool
     */
    public function destroy(Leader $leader,Leader $leader_target)
    {
        if ($leader->organize->id !== $leader_target->organize->id) myResult()->stop(ResultTool::CODE_NO_RIGHT);
        if ($leader_target->isOrganizeAdmin()) myResult()->stop(ResultTool::CODE_TARGET_WRONG,'默认账号不可删除');
        if ($leader->isOrganizeAdmin()) return true;
        if ($leader->id === $leader_target->id) myResult()->stop(ResultTool::CODE_TARGET_WRONG,'不可删除自己');

        myResult()->stop(ResultTool::CODE_NO_RIGHT);
    }
}